package y_2025.m05.d23;

public class TicketSystem {
    private static int tickets = 100; // 总票数
    
    public static void main(String[] args) {
        for (int i = 1; i <= 4; i++) {
            new Thread(new TicketWindow(i), "窗口" + i).start();
        }
    }
    
    static class TicketWindow implements Runnable {
        private final int windowId;
        
        public TicketWindow(int id) {
            this.windowId = id;
        }
        
        @Override
        public void run() {
            while (true) {
                synchronized (TicketSystem.class) {
                    if (tickets <= 0) {
                        System.out.println("窗口" + windowId + ": 票已售完");
                        break;
                    }
                    
                    System.out.println(Thread.currentThread().getName() + 
                                     "售出第" + tickets + "张票");
                    tickets--;
                    
                    try {
                        Thread.sleep(100); // 模拟售票时间
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}